<?php

/**
 * This is the model class for table "articleen".
 *
 * The followings are the available columns in table 'articleen':
 * @property integer $id
 * @property integer $category_id
 * @property integer $member_id
 * @property string $title
 * @property string $content
 * @property string $postdate
 * @property string $quotefrom
 */
class Articleen extends CActiveRecord {

    /**
     * Returns the static model of the specified AR class.
     * @param string $className active record class name.
     * @return Articleen the static model class
     */
    public static function model($className = __CLASS__) {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName() {
        return 'articleen';
    }

    public $excerpt = '';
    public $hasMore = true;

    /**
     * @return array validation rules for model attributes.
     */
    public function rules() {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('id, category_id, member_id', 'numerical', 'integerOnly' => true),
            array('keyword, title, quotefrom', 'length', 'max' => 255),
            array('content, postdate', 'safe'),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('id, keyword, category_id, member_id, title, content, postdate, quotefrom', 'safe', 'on' => 'search'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations() {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
            'category' => array(self::BELONGS_TO, 'Category', 'category_id'),
            'author' => array(self::BELONGS_TO, 'Member', 'member_id'),
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels() {
        return array(
            'id' => 'ID',
            'category_id' => 'Category',
            'member_id' => 'Member',
            'title' => 'Title',
            'content' => 'Content',
            'postdate' => 'Postdate',
            'quotefrom' => 'Quotefrom',
        );
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search() {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria = new CDbCriteria;

        $criteria->compare('id', $this->id);
        $criteria->compare('category_id', $this->category_id);
        $criteria->compare('member_id', $this->member_id);
        $criteria->compare('title', $this->title, true);
        $criteria->compare('content', $this->content, true);
        $criteria->compare('postdate', $this->postdate, true);
        $criteria->compare('quotefrom', $this->quotefrom, true);

        return new CActiveDataProvider($this, array(
            'criteria' => $criteria,
            'pagination' => array(
                'pageSize' => 20,
            ),
        ));
    }

    /**
     * set timestamp before save
     */
    public function beforeSave() {
        if ($this->isNewRecord)
            $this->postdate = new CDbExpression('NOW()');
        return parent::beforeSave();
    }

    /**
     * delete all product details and product images before
     */
    public function beforeDelete() {
        return parent::beforeDelete();
    }

    /**
     * get category name
     */
    public function getCategoryName() {
        return Category::model()->getCategoryNameById($this->category_id);
    }

    function getLastNews() {
        $results = Articleen::model()->findAll(array('limit' => Constant::NUMBER_LAST_NEW,
            'order' => 'postdate'));
        $news = array();
        if (count($results) > 0) {
            foreach ($results as $article) {
                $getExcerpt = Util::getExcerpt($article->content, Constant::NUMBER_EXCERPT_LAST_NEW);
                $article->excerpt = $getExcerpt['excerpt'];
                $article->hasMore = $getExcerpt['hasMore'];
                $article->content = '';
                $news[] = $article;
            }
        }
        return $news;
    }

    function getRelatedNews($categoryId = NULL) {
        $attributes = array();
        if ($categoryId != NULL) {
            $attributes = array('category_id' => $categoryId);
        }  
        $condition = array('limit' => Constant::NUMBER_RELATED_NEW, 'order' => 'postdate');
        $results = Articleen::model()->findAllByAttributes($attributes, $condition);
        $news = array();
        if (count($results) > 0) {
            foreach ($results as $article) {
                $article->content = '';
                $news[] = $article;
            }
        }
        return $news;
    }

    function getNews() {
        $criteria = new CDbCriteria;
        return $this->getNewsWithCondition($criteria);
    }

    function getNewsWithCondition($criteria, $hasPaging = TRUE) {
        $criteria->alias = 'p';
        $criteria->order = 'postdate';

        // count result to display multipages
        $count = Articleen::model()->count($criteria);
        $pages = new CPagination($count);

        // results per page
        if ($hasPaging) {
            $pages->pageSize = Constant::NUMBER_NEW_PER_PAGE;
        } else {
            // no pagging (list all in one page)
            $pages->pageSize = $count;
        }

        $pages->applyLimit($criteria);
        $results = $this->findAll($criteria);

        $news = array();
        if (count($results) > 0) {
            foreach ($results as $article) {
                $getExcerpt = Util::getExcerpt($article->content, Constant::NUMBER_EXCERPT_NEW);
                $article->excerpt = $getExcerpt['excerpt'];
                $article->hasMore = $getExcerpt['hasMore'];
                $article->content = '';
                $news[] = $article;
            }
        }

        return array('news' => $news,
            'pages' => $pages);
    }

}